package aom.ju.ss.view.main

import android.content.Intent
import android.os.Message
import aom.ju.ss.R
import aom.ju.ss.databinding.LoginBinding
import aom.ju.ss.mode.LpKey
import aom.ju.ss.mode.LpKey.TOKEN
import aom.ju.ss.mode.LpKey.USER_INFO
import aom.ju.ss.mode.LpKey.WX_APP_ID
import aom.ju.ss.mode.UserInfo
import aom.ju.ss.mode.UserInfoMode
import aom.ju.ss.mode.WxLoginMode
import aom.ju.ss.utils.*
import aom.ju.ss.utils.http.LP_API.APP_WX_LOGIN
import aom.ju.ss.utils.http.LP_API.GET_USER_INFO
import aom.ju.ss.utils.http.LP_API.LOGIN_CODE
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.view.base.BaseActivity
import com.google.gson.Gson
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.WXAPIFactory

class LoginActivity : BaseActivity() {
    private lateinit var binding: LoginBinding
    override fun init() {
        binding = viewBinding as LoginBinding

        SpUtils.clearAll()
        SpUtils.clearKey(USER_INFO)
        SpUtils.clearKey(TOKEN)
        UserInfo.userInfo=null
        SpUtils.putInt(LpKey.ONE_START, 1)


        val req = SendAuth.Req()
        req.scope = "snsapi_userinfo"
        req.state = "wechat_sdk_demo_test"

        val api = WXAPIFactory.createWXAPI(this, WX_APP_ID)

        binding.wx.setOnClickListener {
            if (!binding.check.isChecked) {
                "请先同意用户隐私协议".show
                return@setOnClickListener
            }
            api.sendReq(req);
        }

        binding.sendCode.setOnClickListener {
            sendCode(binding.phone, binding.sendCode)
        }
        binding.login.setOnClickListener {
            if (!binding.check.isChecked) {
                "请先同意用户隐私协议".show
                return@setOnClickListener
            }

            if (binding.phone.isPhone && binding.edCode.isCode) {
                OKHttpUtils(activity)
                    .SetApiUrl(LOGIN_CODE)
                    .SetKey("phone", "code")
                    .SetValue(
                        binding.phone.text.toString(),
                        binding.edCode.text.toString()
                    ).POST(this)
            }
        }

    }

    override val lay: Int = R.layout.login

    override fun callBack(event: Message) {
        super.callBack(event)
        if (event.arg1 == LpKey.WX_LOGIN_OK) {
            OKHttpUtils(activity)
                .SetApiUrl(APP_WX_LOGIN)
                .setLoadingStr("登陆中...")
                .SetKey("code")
                .SetValue(event.obj.toString())
                .POST(this)

        } else if (event.arg1 == LpKey.WX_LOGIN_ERR) {
            "微信登录失败".show
        } else if (event.arg1 == LpKey.LOGIN_OK) {// 绑定手机号成功

            getUserinfo()

        }
    }

    private fun getUserinfo() {
        OKHttpUtils(activity)
            .SetApiUrl(GET_USER_INFO)
            .setLoadingStr("正在获取登录信息...")
            .POST(this)
    }

    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        when (httpTY) {
            GET_USER_INFO -> {
                val mode = Gson().fromJson(jsonString, UserInfoMode::class.java)
                SpUtils.putString(USER_INFO, Gson().toJson(mode.data))
                startActivity(Intent(activity, MainActivity::class.java))
                finish()
            }

            LOGIN_CODE, APP_WX_LOGIN -> {
                val mode = Gson().fromJson(jsonString, WxLoginMode::class.java)
                SpUtils.putString(LpKey.TOKEN, mode.data.token)
                getUserinfo()
            }
        }
    }

    override fun error(e: String, jsonString: String, httpTY: String) {
        super.error(e, e, httpTY)
        when (httpTY) {
            APP_WX_LOGIN -> {
                val mode = Gson().fromJson(jsonString, WxLoginMode::class.java)
                mode.msg.show
                if (mode.code == 1002) {
                    startActivity(
                        Intent(activity, BandPhoneActivity::class.java).putExtra(
                            "key",
                            mode.data.key
                        )
                    )
                }
            }
            LOGIN_CODE -> {
                val mode = Gson().fromJson(jsonString, WxLoginMode::class.java)
                mode.msg.show
                if (mode.code == 1001) {
//                    TitleDialog.
                }
            }
        }

    }
}